﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Site.ViewModels;
using SDT.Lib.Common;
using SDT.Site.Properties;

namespace SDT.Site.Areas.User.Controllers
{
    [Authorize]
    public class CustomerController : Controller
    {
        //
        // GET: /User/Customer/
        SDTDataContext db = new SDTDataContext();
        public ActionResult Index()
        {
            return View();
        }
        [ChildActionOnly]
        public ActionResult GetLeft(string viewName)
        {
            var c = CustomerManager.GetUser(User.Identity.Name);
            return PartialView(viewName,c);
        }
        public ActionResult Update()
        {
            CustomerEditModels c = new CustomerEditModels();
            c.Customer = CustomerManager.GetUser(User.Identity.Name);
            c.Cities = db.Cities.ToList();
            c.Districts = db.Districts.ToList();
            c.Sexuals = Utils.StringToDictionaryIntString(Settings.Default.CustomerSexual, ",");
            return View(c);
        }
        [HttpPost]
        public ActionResult Update(CustomerEditModels c)
        {
            try
            {
                c.Customer = db.Customers.Where(e => e.UserName.ToLower() == User.Identity.Name.ToLower() || 
                    e.Email.ToLower() == User.Identity.Name.ToLower()).FirstOrDefault();
                if (c.Customer != null)
                {
                    UpdateModel(c);
                    c.Customer.CityID = Utils.ParseInt(Request["ddlCity"]);
                    c.Customer.DistrictID = Utils.ParseInt(Request["ddlDistrict"]);
                    c.Customer.SexID = Utils.ParseInt(Request["rdoSex"]);
                    db.SubmitChanges();
                    return RedirectToAction("Index", "Order", new { area = "User" });
                }
                else {
                    ViewData[Messages.MessageInfo] = Messages.ERR_CUS_NOT_EXIST;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
